package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        Integer offset = (page-1) * (rows);
        Map<String, Object> map = new HashMap<>();
        List<Customer> customers = customerDao.list(offset,rows,customerName);
        int total = customerDao.getTotal();
        map.put("total",total);
        map.put("rows",customers);
        return map;
    }

    @Override
    public void save(Customer customer) {
        if (customer.getCustomerId()==null) {
            customerDao.save(customer);
        }
        customerDao.updateById(customer);
    }

    @Override
    public void delete(String ids) {
        customerDao.delete(ids);
    }

    @Override
    public List<Customer> getComboboxList(String q) {
        return customerDao.list(0,100,q);
    }
}
